package com.ruoyi.invoice.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.invoice.domain.TbInvoice;
import com.ruoyi.invoice.service.InvoiceService;
import com.ruoyi.invoice.util.SendEamilUtil;
import nuonuo.open.sdk.NNOpenSDK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/invoice")
public class InvoiceController extends BaseController {

    @Autowired
    InvoiceService tbInvoiceService;

    /**
     * 查询列表
     */
    @RequiresPermissions("menu:invoice:list")
    @GetMapping("/list")
    public TableDataInfo list(TbInvoice tbInvoice)
    {
        startPage();
        List<TbInvoice> list = tbInvoiceService.selectTbInvoiceList(tbInvoice);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @RequiresPermissions("menu:invoice:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInvoice tbInvoice)
    {
        List<TbInvoice> list = tbInvoiceService.selectTbInvoiceList(tbInvoice);
        ExcelUtil<TbInvoice> util = new ExcelUtil<TbInvoice>(TbInvoice.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取详细信息
     */
    @RequiresPermissions("menu:invoice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbInvoiceService.selectTbInvoiceById(id));
    }

    /**
     * 新增
     */
    @RequiresPermissions("menu:invoice:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInvoice tbInvoice)
    {
        return toAjax(tbInvoiceService.insertTbInvoice(tbInvoice));
    }

    /**
     * 修改
     */
    @RequiresPermissions("menu:invoice:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInvoice tbInvoice)
    {
        return toAjax(tbInvoiceService.updateTbInvoice(tbInvoice));
    }

    /**
     * 删除
     */
    @RequiresPermissions("menu:invoice:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbInvoiceService.deleteTbInvoiceByIds(ids));
    }




    @RequestMapping("/invoice")
    public void invoice(@RequestBody TbInvoice tbInvoice) throws GeneralSecurityException, MessagingException {
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String taxnum = tbInvoice.getTaxMark(); // 授权企业税号
        String appKey = "SD86754078";
        String appSecret = "SD90992CC75040AD";
        String method = "nuonuo.ElectronInvoice.sj"; // API方法名
        String token = "2d484e**************pdui"; // 访问令牌
        String content = "{" +
                "\"outInvoicePrintRequests\": [ " +
                    "{" +
                        "\"userId\":"+tbInvoice.getUserId()+"," +
                        "\"companyName\": \""+tbInvoice.getCompanyName()+"\"," +
                         "\"mailbox\": \""+tbInvoice.getMailbox()+"\"," +
                        "\"money\": "+tbInvoice.getMoney()+" " +
                     "}" +
                    "] " +
                "}";

    String url = "https://sandbox.nuonuocs.cn/open/v1/services"; // SDK请求地址
    String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识，32位随机码，无需修改，保持默认即可
    String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
    SendEamilUtil.sendEamil(result, tbInvoice.getMailbox());
    System.out.println(result);
    }

}
